﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ivory.GUI
{
    public class ImgButton : GUIComponent
    {
        //Fields  
		private Vector2 _ButtonPosition; 
		public Vector2 ButtonPosition
		{
			get { return _ButtonPosition; }
            set { _ButtonPosition = value; this.ResizeComponent(); }
		}

        private Color _ButtonColor = Color.White; 
		public Color ButtonColor
		{
			get { return _ButtonColor; }
			set { _ButtonColor = value; }
		} 		
 
		private Color _ButtonHoverColor = Color.Black; 
		public Color ButtonHoverColor
		{
			get { return _ButtonHoverColor; }
			set { _ButtonHoverColor = value; }
		}

        protected Color ButtonRenderColor = Color.White;
 
		private Texture2D _ButtonTexture; 
		public Texture2D ButtonTexture
		{
			get { return _ButtonTexture; }
            set { _ButtonTexture = value; this.ResizeComponent(); }
		}

        protected string TextureLocation = string.Empty;

        //Construct
        public ImgButton(Game game, string textureLocation, Vector2 Position)
            : base(game, new Rectangle((int)Position.X, (int)Position.Y, 0, 0))
        {
            this.TextureLocation = textureLocation;
            this.ButtonPosition = Position;

            this.OnMouseEnter += new EventHandler(OnMouseIn);
            this.OnMouseLeave += new EventHandler(OnMouseOut);
        }
        
        public ImgButton(Game game, Texture2D texture, Vector2 Position)
            : base(game, new Rectangle((int)Position.X, (int)Position.Y, texture.Width, texture.Height))
        {
            this.ButtonTexture = texture;
            this.ButtonPosition = Position;

            this.OnMouseEnter += new EventHandler(OnMouseIn);
            this.OnMouseLeave += new EventHandler(OnMouseOut);
        }
        
        //Methods
        protected override void ResizeComponent()
        {
            ClickRectangle = new Rectangle((int)ButtonPosition.X, (int)ButtonPosition.Y, ButtonTexture.Width, ButtonTexture.Height);
        }

        public void OnMouseIn(object sender, EventArgs e)
        {
            ButtonRenderColor = ButtonHoverColor;
        }
        public void OnMouseOut(object sender, EventArgs e)
        {
            ButtonRenderColor = ButtonColor;
        }

        public override void DrawComponent(SpriteBatch batch, GameTime time)
        {
            batch.Draw(ButtonTexture, ClickRectangle, ButtonRenderColor);
        }
        public override void LoadComponent()
        {
            if (string.IsNullOrEmpty(TextureLocation))
            {
                if(ButtonTexture == null)
                    throw new IvoryFrameworkError(Errors.AssetLoadingException);
            }

            ButtonTexture = Game.Content.Load<Texture2D>(TextureLocation);
        }
    }
}
